/*********************** (c) 2000 STMicroelectronics *************************
PROJECT : CHERY B21
COMPILER : Metrowerks Codewarrior
MODULE : can.h
VERSION : V 1.1
ST7 pCAN PERIPHERAL DRIVER
AUTHOR : Central Europe 8bit Micro Application Group
MODIFICATIONS : none*/

#ifndef CAN_H
#define CAN_H
#define CAN_MAX_DATA_SIZE 8
#ifndef _H_STDDEF_
#define NULL (void*) 0 /*Not already defined if stddef.h not included*/
#endif

#ifndef _CANVAR
#define can_extern extern
#else
#define can_extern
#endif

/* TYPEDEF */
//Simple data types//
typedef enum 
{
	BUS_WAKEUP,
	SOFT_WAKEUP
}WakeUp_Cause;

typedef enum
{
	DLC0,
	DLC1,
	DLC2,
	DLC3,
	DLC4,
	DLC5,
	DLC6,
	DLC7,
	DLC8,
	REMOTE_FRAME
}CAN_Data_Size;

//Error status messages
typedef enum 
{
	CAN_INIT_SUCCESS,
	CAN_INIT_FAILURE
}CAN_Init_Error;//Initialisation

typedef enum
{
	CAN_SLEEP_FATAL,
	CAN_SLEEP_ERROR,
	CAN_SLEEP_SUCCESS,
	CAN_SWITCH_ON_SUCCES,
	CAN_SWITCH_ON_FAILURE
}CAN_Switch_Error;//Sleep

typedef enum
{
	CAN_RUN,
	CAN_STANDBY,
	CAN_BUS_ACTIVE,
	CAN_BUS_PASSIVE,
	CAN_BUS_OFF
}CAN_Status;//Status 

typedef enum 
{
	CFALSE=0,
	CTRUE=1
}CAN_Bool;//Misc.

//Transmission-Reception
typedef enum
{
	CAN_TRANSMIT_FATAL,
	CAN_TRANSMIT_SUCCESS,
	CAN_TRANSMIT_FAILURE,
	CAN_TRANSMIT_NO_MSG,
	CAN_FIFO_FULL,
	CAN_TRANSMIT_BUFFER_FULL,
	CAN_TRANSMISSION_ERROR_IT
}CAN_Transmit_Error;

typedef enum
{
	CAN_RECEIVE_FATAL,
	CAN_RECEIVE_SUCCESS,
	CAN_RECEIVE_REMOTE,
	CAN_ILLEGAL_IDENTIFIER,
	CAN_FILTERING_FAILURE,
	CAN_BUFFER_IN_USE,
	CAN_NO_BUFFER,
	CAN_RCV_BUFFER_NOT_READY
}CAN_Receive_Error;

//Transmission/Reception structures //

//Input/output structure
typedef struct CAN_Buffer{
	uword msg_identifier;//12 most significant bits
	CAN_Data_Size data_size;
	uchar CAN_msg_data[CAN_MAX_DATA_SIZE];
	CAN_Bool buffer_rw;//if true cannot be read or written otherwise can be used 
	CAN_Bool buffer_free; //shared variable If CTRUE, the data can be modified.
}CAN_Buffer;

//Initialization parameters//

//Initialisation data structure
typedef struct {
	uchar brpr_init;
	uchar btr_init;
	uchar fhr1_init;
	uchar flr1_init;
	uchar mhr1_init;
	uchar mlr1_init;
	uchar fhr0_init;
	uchar flr0_init;
	uchar mhr0_init;
	uchar mlr0_init;
}Init_Data;
typedef Init_Data* Init_Data_Ptr; // Pointer on an Init_Data structure

/* VARIABLES */
//Initialization of the CAN-cell
//Variables declared in can.c
extern const Init_Data first_init_data; //Initialisation datas used at power-on
extern const uword i_filters[]; // Array of accepted filters
extern unsigned can_err_flag1;

uword message_ident;


#define CAN_err_orif		0
#define CAN_err_teif		1
#define CAN_err_tran		2
#define CAN_err_rece		3
extern unsigned can_err_flag2;
#define CAN_err_change_act	0		//in active state
#define CAN_err_change_epsv	1		//in error passive state
#define CAN_err_change_boff	2		//in bus off state
#define CAN_err_change_std	3		//in standby state

/* FUNCTIONS */

//Power-on initialisation

extern void CAN_Interrupt(void);
CAN_Init_Error CAN_First_Init(void);
//Resets IT authorization flags
void CAN_It_Dis(void);
//Gets state of the cell : active, passive, off
CAN_Status CAN_Get_Status (void);
//Gets value of Transmit Error Counter & Reception Error Counter
uchar CAN_Get_TEC(void);
uchar CAN_Get_REC(void);
//Modification of the cell state
 CAN_Switch_Error CAN_Switch_Off(WakeUp_Cause);
CAN_Switch_Error CAN_Sleep (WakeUp_Cause);
extern CAN_Switch_Error CAN_Switch_On(Init_Data_Ptr,CAN_Bool);
//Transmission request
 CAN_Transmit_Error CAN_Transmit_Request(CAN_Buffer*);
extern void  can_message_clean(void);
/* MACROS */
/*-------------------------REGISTER BITS DEFINITION--------------------------*/

#define RXIF3 7 /* Interrupt Status Register */
#define RXIF2 6
#define RXIF1 5
#define TXIF 4
#define SCIF 3
#define ORIF 2
#define TEIF 1
#define EPND 0
#define ESCI 6 /* Interrupt Control Register */
#define RXIE 5
#define TXIE 4
#define SCIE 3
#define ORIE 2
#define TEIE 1
#define ETX 0
#define BOFF 6 /* Control Status Register */
#define EPSV 5
#define SRTE 4
#define NRTX 3
#define FSYN 2
#define WKPS 1
#define RUN 0
#define RJW1 7 /* Baud Rate Prescaler Register */
#define RJW0 6
#define BRP5 5
#define BRP4 4
#define BRP3 3
#define BRP2 2
#define BRP1 1
#define BRP0 0
#define BS22 6 /* Bit Timing Register */
#define BS21 5
#define BS20 4
#define BS13 3
#define BS12 2
#define BS11 1
#define BS10 0
#define PAGE0 0 /* Page Selection Register */
#define PAGE1 1
#define PAGE2 2
#define PAGE3 3
#define PAGE4 4
#define RTR 4
#define MSK_DLC 0x0F /* Identifier Low Register */
#define ACC 3 /* Buffer Control Status Register */
#define RDY 2 /* Buffer Control Status Register */
#define BUFBUSY 1
#define BUFLOCK 0
/*------------------------------------------------------------------------*/
//uword  recept_counter;
//To clear status registers flags
#define Clr_RXIF3_Bit() (CANISR &= 0x7F)
#define Clr_RXIF2_Bit() (CANISR &= 0xBF)
#define Clr_RXIF1_Bit() (CANISR &= 0xDF)
#define Clr_TXIF_Bit() (CANISR &= 0xEF)
#define Clr_SCIF_Bit() (CANISR &= 0xF7)
#define Clr_ORIF_Bit() (CANISR &= 0xFB)
#define Clr_TEIF_Bit() (CANISR &= 0xFD)
#define Clr_EPND_Bit() (CANISR &= 0xFE)
//To clear
#define Clr_LOCK_Bit() (CANBCSR &= 0xFE)
#define Clr_RDY_Bit() (CANBCSR &= 0xFA)
#define Size_Of_Words_Array(array) sizeof(array)/sizeof(unsigned int)
/*----------------------CAN SETTINGS----------------------*/
#define CAN_RUN_Cell() (CANCSR |= 0x05)
#endif
/****** (c) 2000 STMicroelectronics ********************* END OF FILE _******/
